﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows;
using System.IO;

namespace PatientInformationExtract.MainInterface.Models
{
    public sealed class Logger
    {
        public enum LogType
        {
            Info,
            Export,
            Translate,
        }

        public enum LogStatus
        {
            Succeed,
            Failure,
            Null,
        }

        private readonly static DataTable _logTable;

        static Logger()
        {
            _logTable = new DataTable();
            _logTable.Columns.Add("Time", typeof(DateTime));
            _logTable.Columns.Add("Type", typeof(LogType));
            _logTable.Columns.Add("Status", typeof(LogStatus));
            _logTable.Columns.Add("Message", System.Type.GetType("System.String"));
        }

        Logger()
        {
        }

        public static void Info(string message, LogStatus status)
        {
            WriteLog(message, LogType.Info, status);
        }

        public static void Info(string message)
        {
            Info(message, LogStatus.Null);
        }

        public static void ExportInfo(string message, LogStatus status)
        {
            WriteLog(message, LogType.Export, status);
        }

        public static void ExportInfo(string message)
        {
            ExportInfo(message, LogStatus.Null);
        }

        public static void Translate(string message, LogStatus status)
        {
            WriteLog(message, LogType.Translate, status);
        }

        public static void Translate(string message)
        {
            Translate(message, LogStatus.Null);
        }

        public static void Error(string message)
        {
            WriteLog(message, LogType.Info, LogStatus.Failure);
        }

        public static void WriteLog(string message, LogType type, LogStatus status)
        {
            DataRow dr = _logTable.NewRow();
            dr["Time"] = DateTime.Now;
            dr["Type"] = type;
            dr["Status"] = status;
            dr["Message"] = message;
            _logTable.Rows.Add(dr);
        }

        public static void Clear()
        {
            _logTable.Rows.Clear();
        }

        public static void SaveAll(string savePath)
        {
            FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            foreach (DataRow dr in _logTable.Rows)
            {
                string buffer = ((DateTime)dr["Time"]).ToString("yyyy-MM-dd HH:mm:ss") + " " + ((LogType)dr["Type"]).ToString();
                if (((LogStatus)dr["Status"]).Equals(LogStatus.Null))
                {
                    buffer += ": " + dr["Message"];
                }
                else
                {
                    buffer += "[" + ((LogStatus)dr["Status"]).ToString() + "]: " + dr["Message"];
                }
                sw.WriteLine(buffer);
            }
            sw.Close();
            fs.Close();
        }

        public static void SaveInfo(string savePath)
        {
            FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            foreach (DataRow dr in _logTable.Rows)
            {
                if (((LogType)dr["Type"]).Equals(LogType.Info))
                {
                    string buffer = ((DateTime)dr["Time"]).ToString("yyyy-MM-dd HH:mm:ss") + " " + ((LogType)dr["Type"]).ToString();
                    if (((LogStatus)dr["Status"]).Equals(LogStatus.Null))
                    {
                        buffer += ": " + dr["Message"];
                    }
                    else
                    {
                        buffer += "[" + ((LogStatus)dr["Status"]).ToString() + "]: " + dr["Message"];
                    }
                    sw.WriteLine(buffer);
                }
            }
            sw.Close();
            fs.Close();
        }

        public static void SaveExportInfo(string savePath)
        {
            FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            foreach (DataRow dr in _logTable.Rows)
            {
                if (((LogType)dr["Type"]).Equals(LogType.Export))
                {
                    string buffer = ((DateTime)dr["Time"]).ToString("yyyy-MM-dd HH:mm:ss") + " " + ((LogType)dr["Type"]).ToString();
                    if (((LogStatus)dr["Status"]).Equals(LogStatus.Null))
                    {
                        buffer += ": " + dr["Message"];
                    }
                    else
                    {
                        buffer += "[" + ((LogStatus)dr["Status"]).ToString() + "]: " + dr["Message"];
                    }
                    sw.WriteLine(buffer);
                }
            }
            sw.Close();
            fs.Close();
        }

        public static void SaveTranslateInfo(string savePath)
        {
            FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            foreach (DataRow dr in _logTable.Rows)
            {
                if (((LogType)dr["Type"]).Equals(LogType.Translate))
                {
                    string buffer = ((DateTime)dr["Time"]).ToString("yyyy-MM-dd HH:mm:ss") + " " + ((LogType)dr["Type"]).ToString();
                    if (((LogStatus)dr["Status"]).Equals(LogStatus.Null))
                    {
                        buffer += ": " + dr["Message"];
                    }
                    else
                    {
                        buffer += "[" + ((LogStatus)dr["Status"]).ToString() + "]: " + dr["Message"];
                    }
                    sw.WriteLine(buffer);
                }
            }
            sw.Close();
            fs.Close();
        }

    }
}
